package com.thinkfit.jy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

public class DialogActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    public static final int SHOW_WIFI_DISCONNECT_DIALOG = 1;//显示wifi断开对话框
    public static final int SHOW_INPUT_WIFI_PSW_DIALOG = 2;//显示输入wifi密码对话框
    public static final int SHOW_LOGOUT = 3;//显示退出登录对话框

    public static final int END_SPORT_BACK = 4;//显示退出运动对话框
    public static final int END_SPORT_TO_HOME = 5;//显示退出运动对话框

    public static final int TARGET_COMPLETE = 6;//运动结束提示是否退出对话框
    public static final int SHOW_VERSION = 7;//提示更新版本对话框
    EditText etPsw;
    TextView tvInfo;
    View v1;
    private int mType;

    private int mCurrentStyle;

    @Override
    protected int getLayoutId() {
        if (mType == SHOW_INPUT_WIFI_PSW_DIALOG) {
//            return mCurrentStyle == 3 ? R.layout.activity_input_wifi_dialog_xm : R.layout.activity_input_wifi_dialog;
            return R.layout.activity_input_wifi_dialog;
        }
        return R.layout.activity_input_wifi_dialog;
//        return mCurrentStyle == 3 ? R.layout.activity_dialog_xm : R.layout.activity_dialog;
    }

    public void onCreate(Bundle savedInstanceState) {
        mType = getIntent().getIntExtra(Constants.BUNDLE_EXTRA, 0);
        mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        getWindow().setAttributes(params);
        if (mCurrentStyle == 3) {
            Bitmap bmp = (Bitmap) DataManager.Companion.getInstance().getObject();
            getWindow().setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp));
        } else {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
        etPsw = findViewById(R.id.etPsw);
        tvInfo = findViewById(R.id.tvInfo);
        if(mType == SHOW_INPUT_WIFI_PSW_DIALOG){
            etPsw.setVisibility(View.VISIBLE);
            tvInfo.setVisibility(View.GONE);
        }else {
            etPsw.setVisibility(View.GONE);
            tvInfo.setVisibility(View.VISIBLE);
        }

        v1 = findViewById(R.id.v_1);
        setViewsOnClickListener(R.id.btn1, R.id.btn2,R.id.v_1);
        switch (mType) {
            case SHOW_WIFI_DISCONNECT_DIALOG:
                setText(R.id.tv1, getString(R.string.app_dialog_title));
                setText(R.id.tvInfo, R.string.app_are_you_sure_disconnect_wifi);
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
            case SHOW_INPUT_WIFI_PSW_DIALOG:
                String wifiName = getIntent().getStringExtra(Constants.BUNDLE_EXTRA_2);
                setText(R.id.tv1, getString(R.string.app_input_xxx_wifi_psw, wifiName));
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
            case SHOW_LOGOUT:
                setText(R.id.tv1, getString(R.string.app_dialog_title));
                setText(R.id.tvInfo, R.string.app_are_you_sure_logout);
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
            case END_SPORT_BACK:
            case END_SPORT_TO_HOME:
                setText(R.id.tv1, getString(R.string.app_dialog_title));
                setText(R.id.tvInfo, R.string.app_sure_end_sport);
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
            case TARGET_COMPLETE:
                setText(R.id.tv1, getString(R.string.app_dialog_title));
                setText(R.id.tvInfo, R.string.app_target_complete_stop_run);
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
            case SHOW_VERSION:
                setText(R.id.tv1, getString(R.string.app_dialog_title));
                setText(R.id.tvInfo, R.string.app_find_new_version);
                setText(R.id.btn1, R.string.app_cancel);
                setText(R.id.btn2, R.string.app_ok);
                break;
        }

        etPsw.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 软键盘显示
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx    软键盘显示");
                    SportsStatusBarViewUtils.getInstance().goneLayout();
                } else {
                    // 软键盘隐藏
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx    软键盘隐藏");
                    SportsStatusBarViewUtils.getInstance().visibleLayout();
                }
            }
        });
        etPsw.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_ENTER){
                    // 获取InputMethodManager
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    // 隐藏键盘
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    etPsw.clearFocus();
                }
                return false;
            }
        });
    }



    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   DialogActivity ");
        DeviceTypeUtils.showBackButton(getApplicationContext());
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn2:
                if (mType == SHOW_LOGOUT) {
                    DataManager.Companion.getInstance().logout();
                    toHomeActivity();
                    return;
                } else if (mType == END_SPORT_BACK) {
                    //停止串口
                    if(DeviceTypeUtils.connectDeviceType() == 0){
                        if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
                            BluetoothOrder.getInstance().stopTreadmill();
                        }else {
                            BluetoothOrder.getInstance().stopPlay();
                        }
                    }else {
                        if(MotionDataUtils.getInstance().isRun()){
                            SerialPortManager.getInstance().stopRun();
                        }
                    }
                    HashMap<String, Object> map = new HashMap<>();
                    map.put(EventBusEvent.KILL_ACTIVITY,"");
                    EventBus.getDefault().post(map);
                    finish();
                    return;
                } else if (mType == END_SPORT_TO_HOME || mType == TARGET_COMPLETE) {
                    //停止串口
                    if(DeviceTypeUtils.connectDeviceType() == 0){
                        BluetoothOrder.getInstance().stopPlay();
                    }else {
                        SerialPortManager.getInstance().stopRun();
                    }
                    openActivity(MainActivity.class);
                    toHomeActivity();
                    return;
                }else if(mType == SHOW_VERSION){
                    openActivity(VersionActivity.class);
                }
                Intent intent = new Intent();
                intent.putExtra(Constants.BUNDLE_EXTRA, mType);
                if (mType == SHOW_INPUT_WIFI_PSW_DIALOG) {
                    String psw = getTextById(R.id.etPsw);
                    if (TextUtils.isEmpty(psw)) {
                        showToast(getTextById(R.id.tv1));
                        return;
                    }
                    intent.putExtra(Constants.BUNDLE_EXTRA_2, psw);
                }
                setResult(Activity.RESULT_OK, intent);
                etPsw.clearFocus();
                finish();
                break;
            case R.id.btn1:
            case R.id.v_1:
                etPsw.clearFocus();
                finish();
                break;
        }
    }
}